ডেটা স্ট্রাকচারগুলি হল এমন কাঠামো যা ডেটাকে সুশৃঙ্খলভাবে সংরক্ষণ এবং পরিচালনা করতে সাহায্য করে। এগুলি বিভিন্ন প্রোগ্রামিং সমস্যার সমাধানে সহায়ক হয়। এখানে Linked List, Stack, Queue, এবং Hash Table এর সংজ্ঞা এবং তাদের প্রয়োগগুলো আলোচনা করা হলো।
Linked List একটি ডেটা স্ট্রাকচার যা একাধিক উপাদান (node) নিয়ে গঠিত। প্রতিটি node এ ডেটা এবং পরবর্তী node এর একটি রেফারেন্স থাকে। এটি অ্যারের তুলনায় অনেক বেশি ফ্লেক্সিবল, কারণ এটি ডায়নামিকভাবে মেমোরি বরাদ্দ করতে সক্ষম।
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node *next;
};
// Function to insert at the beginning
void insert(struct Node **head, int value) {
struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
new_node->data = value;
new_node->next = *head;
*head = new_node;
}
// Function to display the list
void display(struct Node *head) {
struct Node *temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node *head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
display(head); // Output: 30 -> 20 -> 10 -> NULL
return 0;
}
Stack একটি ডেটা স্ট্রাকচার যা Last In, First Out (LIFO) প্রিন্সিপল অনুসরণ করে। এতে উপাদানগুলি একে একে উপরে চাপানো (push) এবং উপরের উপাদানটি প্রথমে অপসারণ (pop) করা হয়।
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int stack[MAX];
int top = -1;
// Push operation
void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = value;
printf("Pushed %d\n", value);
}
}
// Pop operation
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
} else {
return stack[top--];
}
}
int main() {
push(10);
push(20);
push(30);
printf("Popped: %d\n", pop()); // Output: 30
printf("Popped: %d\n", pop()); // Output: 20
return 0;
}
Queue একটি ডেটা স্ট্রাকচার যা First In, First Out (FIFO) প্রিন্সিপল অনুসরণ করে। এতে উপাদানগুলি একটি প্রান্তে যোগ করা (enqueue) হয় এবং অন্য প্রান্ত থেকে অপসারণ করা (dequeue) হয়।
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
// Enqueue operation
void enqueue(int value) {
if (rear == MAX - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1) front = 0;
queue[++rear] = value;
printf("Enqueued %d\n", value);
}
}
// Dequeue operation
int dequeue() {
if (front == -1) {
printf("Queue Underflow\n");
return -1;
} else {
int value = queue[front++];
if (front > rear) front = rear = -1; // Reset queue if empty
return value;
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
printf("Dequeued: %d\n", dequeue()); // Output: 10
printf("Dequeued: %d\n", dequeue()); // Output: 20
return 0;
}
Hash Table একটি ডেটা স্ট্রাকচার যা একটি কী-ভ্যালু পেয়ার সংরক্ষণ করে। এটি hash function ব্যবহার করে ইনডেক্স তৈরি করে, যাতে দ্রুত ডেটা অ্যাক্সেস করা যায়। এটি একটি কার্যকরী ডেটা স্ট্রাকচার যা দ্রুত অনুসন্ধান, ইনসার্ট এবং ডিলিট অপারেশন করে।
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
// Hash function to generate index
int hash(int key) {
return key % SIZE;
}
void insert(int *hashTable, int key, int value) {
int index = hash(key);
hashTable[index] = value;
printf("Inserted key %d at index %d\n", key, index);
}
int search(int *hashTable, int key) {
int index = hash(key);
return hashTable[index];
}
int main() {
int hashTable[SIZE] = {0};
insert(hashTable, 1, 100);
insert(hashTable, 2, 200);
insert(hashTable, 3, 300);
printf("Value for key 1: %d\n", search(hashTable, 1)); // Output: 100
printf("Value for key 2: %d\n", search(hashTable, 2)); // Output: 200
return 0;
}
এখানে, hash()
ফাংশনটি একটি কী নিয়ে তা হ্যাশ করে ইনডেক্স তৈরি করেছে এবং সেই ইনডেক্সে মান সংরক্ষণ করেছে।
ডেটা স্ট্রাকচার | সংজ্ঞা | প্রয়োগ |
---|---|---|
Linked List | একটি ডায়নামিক ডেটা স্ট্রাকচার, যেখানে প্রতিটি নোড পরবর্তী নোডের রেফারেন্স রাখে। | ডায়নামিক মেমোরি, স্ট্যাক, কিউ, গ্রাফের edges। |
Stack | LIFO (Last In First Out) প্রিন্সিপল অনুসরণ করে। | ফাংশন কল ট্র্যাকিং, undo/redo, এক্সপ্রেশন ইভ্যালুয়েশন। |
Queue | FIFO (First In First Out) প্রিন্সিপল অনুসরণ করে। | প্রোসেস শিডিউলিং, BFS, প্রিন্টার শেডিউলিং। |
Hash Table | কী-ভ্যালু |
পেয়ার সংরক্ষণ করা। | ডেটাবেস ইনডেক্সিং, ক্যাশিং, ইউনিক ডেটা স্টোরেজ। |
এই ডেটা স্ট্রাকচারগুলি বিভিন্ন ধরনের প্রোগ্রামিং সমস্যার সমাধানে ব্যবহৃত হয়, যেমন ডেটা সংরক্ষণ, দ্রুত অ্যাক্সেস, এবং কার্যকরী সিস্টেম ডিজাইন।
common.read_more